Skip to content

src: add event loop based metrics into node#62935

Open
pabloerhard wants to merge 2 commits intonodejs:mainfrom
pabloerhard:pabloerhard/add-event-loop-metrics
Open

src: add event loop based metrics into node#62935
pabloerhard wants to merge 2 commits intonodejs:mainfrom
pabloerhard:pabloerhard/add-event-loop-metrics

Conversation

@pabloerhard
Copy link
Copy Markdown

@pabloerhard pabloerhard commented Apr 24, 2026

Adds an opt in into monitorEventLoopDelay(), this option adds support for per iteration sampling using libuv hooks, while preserving the existing timer based approach.

When the new samplePerIteration option is set to true, monitorEventLoopDelay() samples directly from event loop iterations instead of using the interval timer. When samplePerIteration is omitted or false, the existing interval based implementation remains unchanged, including resolution handling and the older interval based trace counters.

Motivation
The interval based implementation has two limitations that the new option addresses:

  • Inaccurate measurements. Delay was approximated from a timer dispatch jitter (RecordDelta() between timer ticks) rather than measured from the loop itself. Samples had an implicit floor of resolution ms and could miss delays that didn't happen to coincide with a timer tick.
  • Wasteful wake ups. The timer forced the event loop to iterate every resolution ms just to record a sample, even when the application was otherwise idle and no one was reading the histogram.

This is an alternative implementation to the one in #62934

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Apr 24, 2026
Comment thread src/histogram.cc Outdated
Comment thread src/histogram.cc
}

uv_close(reinterpret_cast<uv_handle_t*>(&prepare_handle_),
PrepareCloseCB);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should call the superclass's method instead of duplicating it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling HandleWrap::Close() would just uv_close check_handle_ immediately, run HandleWrap::OnClose and treat the wrap as fully torn down.

This would leave prepare_handle_ on need of a uv_close, so you’d risk a libuv handle leak or teardown in the wrong order relative to HandleWrap’s lifecycle.

@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from edb66ea to e7093c8 Compare April 28, 2026 20:12
@pabloerhard pabloerhard marked this pull request as ready for review April 29, 2026 18:38
add a samplePerIteration option to monitorEventLoopDelay that records
event loop delay from libuv event loop iterations instead of the existing timer
interval sampler. The default remains the interval based, uses of monitorEventLoopDelay() will still behave the same unless the samplePerIteration options is passed through.

Signed-off-by: Pablo Erhard Hernandez <[email protected]>
Add test coverage for the per iteration event loop delay histogram start
and stop callbacks

Use disitinct debug tracking keys for the event loop delay histogram
callbacks.
@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from 6f5cdca to bc0a59c Compare April 30, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants